#!/usr/bin/env python
# coding: utf-8

# In[2]:


import numpy as np
import cv2 
import json
import os

L = []

#src_dir = "./1image_reinforce/"
#save_dir = "./7seg_image_real_600_reinforce/"

src_dir = "./1label_4_reinforce/"
save_dir = "./7seg_label_real_600_4class_reinforce/"

overlap = 8
#SIZE = (766,640)   # 2448x2048
#seg_size = (400,320)
#pad_w = 17
#pad_h = 0

SIZE = (1148,960)
seg_size = (592,480)
pad_w = (seg_size[0]*2-SIZE[0])//2
pad_h = 0
# SIZE = (1530,1280)
# seg_size = (800,640)
# pad_w = 35
# pad_h = 0



color = (0,0,0)  #(128,128,128)
for root,dirs,files in os.walk(src_dir):
    for file in files:
        if os.path.splitext(file)[1]=='.png':
            L.append(file)
        elif os.path.splitext(file)[1]=='.jpg':
            L.append(file)
# 3028x2360   (2048,2560) 1996    
for index in range(len(L)):
#for index in range(1):
    pic = cv2.imread( src_dir+L[index],-1)
    if('label' in src_dir):
        pic2 = cv2.resize(pic,SIZE,interpolation=cv2.INTER_LINEAR)
        pic2 = (pic2//25)*25
        print(np.unique(pic2))
    else:
        pic2 = cv2.resize(pic,SIZE,interpolation=cv2.INTER_LINEAR)
        #pic2 = ((pic2 / 25).round() * 25).astype('uint8')
    top,bottom = (overlap+pad_h,overlap+pad_h)
    left,right = (overlap+pad_w,overlap+pad_w)
    pic3 = cv2.copyMakeBorder(pic2,top,bottom,left,right,cv2.BORDER_CONSTANT,value=color)
    #cv2.imwrite("temp.bmp",pic3)
    high = pic3.shape[0]
    width= pic3.shape[1]

    for i in range(high//seg_size[1]):
        for j in range(width//seg_size[0]):
            tmp = pic3[i*seg_size[1]:(i+1)*seg_size[1]+2*overlap,j*seg_size[0]:(j+1)*seg_size[0]+2*overlap]
            if('label' in src_dir):
                cv2.imwrite(save_dir+L[index].split(".")[0]+"_"+str(i)+"_"+str(j)+".png",tmp)
            else:
                cv2.imwrite(save_dir + L[index].split(".")[0] + "_" + str(i) + "_" + str(j) + ".jpg", tmp)
print("finish!")
#cv2.imwrite("show/"+result['imagePath'],showpic)
#cv2.imwrite("label/"+result['imagePath'],mask)
# In[6]:


#cv2.imwrite("mask/"+result['imagePath'],mask)

# In[1]:
#showpic = ((pic)*((255-mask)/255*0.25+mask/255)).astype("uint8")
#colors = [[np.random.randint(0,255)for _ in range(3)]for _ in range(10)]
#showpic = (pic+mask/255*color).astype("uint8")


#cv2.imshow("showpic",showpic)
#cv2.waitKey(0)





